﻿/// <reference path="../Core.ts" />

module Supernova {

    var STARFIELD_STAR_COUNT = 1250;

    export class StarField {

        starFieldCanvas = null;

        starFieldContext = null;

        constructor() {

            this.starFieldCanvas = document.createElement('canvas');
            this.starFieldCanvas.width = 640;
            this.starFieldCanvas.height = 480;

            this.starFieldContext = this.starFieldCanvas.getContext('2d');

            for (var i = 0; i < STARFIELD_STAR_COUNT; i++) {
                var x = Math.random() * this.starFieldCanvas.width;
                var y = Math.random() * this.starFieldCanvas.height;
                var size = Math.floor(Math.random() * 3);
                this.starFieldContext.fillStyle = 'rgb(80, 80, 80)';
                this.starFieldContext.fillRect(x, y, size, size);
            }
        }

        render() {
            var startX = 0;
            var startY = 0;
            if (galaxy !== null) {
                startX = -galaxy.currentView.x * 5;
                startY = -galaxy.currentView.y * 5;
                while (startX > 0) startX -= this.starFieldCanvas.width;
                while (startY > 0) startY -= this.starFieldCanvas.height;
            }
            for (var y = startY; y < canvas.height; y += this.starFieldCanvas.height) {
                for (var x = startX; x < canvas.width; x += this.starFieldCanvas.width) {
                    context.drawImage(this.starFieldCanvas, x, y);
                }
            }
        }

    }

}